package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DataBase.UserInfo;
import DataBase.UserInfoManager;

public class LoginServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public LoginServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		String select = request.getParameter("select");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if (select.equals("普通用户")) {
			ArrayList<UserInfo> users = UserInfoManager.select(username);
			if (users.size() == 0) {
				request.setAttribute("error","用户名或密码错误");
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("/Jsp/index.jsp");
				dispatcher.forward(request, response);
			} else if (users.size() > 1) {

				RequestDispatcher dispatcher = request
						.getRequestDispatcher("/Jsp/index.jsp");
				dispatcher.forward(request, response);
			} else {
				if (users.get(0).getUser_pwd().equals(password)) {
					RequestDispatcher dispatcher = request
							.getRequestDispatcher("/Jsp/frame/frame_user.jsp");
					dispatcher.forward(request, response);
				} else {
					request.setAttribute("error","用户名或密码错误");
					RequestDispatcher dispatcher = request
							.getRequestDispatcher("/Jsp/index.jsp");
					dispatcher.forward(request, response);
				}
			}
		} else {
			request.setAttribute("error","当前只支持普通用户登陆");
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/Jsp/index.jsp");
			dispatcher.forward(request, response);
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// PrintWriter out=response.getWriter();
		// out.write("tess");
		// out.flush();
		// out.close();
		request.setCharacterEncoding("utf-8");
		String select = request.getParameter("select");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if (select.equals("普通用户")) {
			ArrayList<UserInfo> users = UserInfoManager.select(username);
			if (users.size() == 0) {
				request.setAttribute("error","用户名或密码错误");
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("/Jsp/index.jsp");
				dispatcher.forward(request, response);
			} else if (users.size() > 1) {

				RequestDispatcher dispatcher = request
						.getRequestDispatcher("/Jsp/index.jsp");
				dispatcher.forward(request, response);
			} else {
				if (users.get(0).getUser_pwd().equals(password)) {
					RequestDispatcher dispatcher = request
							.getRequestDispatcher("/Jsp/frame/frame_user.jsp");
					dispatcher.forward(request, response);
				} else {
					request.setAttribute("error","用户名或密码错误");
					RequestDispatcher dispatcher = request
							.getRequestDispatcher("/Jsp/index.jsp");
					dispatcher.forward(request, response);
				}
			}
		} else {
			request.setAttribute("error","当前只支持普通用户登陆");
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/Jsp/index.jsp");
			dispatcher.forward(request, response);
		}

	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
